//
//  RegistVerifyView.m
//  Safflower
//
//  Created by 袁亮 on 2016/12/13.
//  Copyright © 2016年 Migic_Z. All rights reserved.
//

#import "RegistVerifyView.h"

static NSInteger _second = 60;

@interface RegistVerifyView()

@property (nonatomic, strong) UITextField *phone_field;
@property (nonatomic, strong) UITextField *verify_code_field;

@property (nonatomic, strong) UIButton *again_send_button;
@property (nonatomic, strong) UIButton *next_step_button;

@property (nonatomic, strong) NSTimer *timer;

@end

@implementation RegistVerifyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self makeRegistVerifyView];
        
        
    }
    return self;
}

- (void)makeRegistVerifyView
{
    UIView *phoneView = [UIView new];
    phoneView.layer.masksToBounds = YES;
    phoneView.layer.borderWidth = 1;
    phoneView.layer.cornerRadius = 10;
    phoneView.layer.borderColor = [[UIColor blackColor] CGColor];
    [self addSubview:phoneView];
    [phoneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH / 3, 36));
        make.centerY.mas_equalTo(self.mas_centerY).mas_offset(-56);
        make.centerX.mas_equalTo(self);
    }];
    
    _phone_field = [UITextField new];
    _phone_field.placeholder = @"请输入手机号码";
    _phone_field.keyboardType = UIKeyboardTypeNumberPad;
    _phone_field.font = [UIFont systemFontOfSize:17.0f];
    [phoneView addSubview:_phone_field];
    [_phone_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    UIView *verifyCodeView = [UIView new];
    verifyCodeView.layer.masksToBounds = YES;
    verifyCodeView.layer.borderWidth = 1;
    verifyCodeView.layer.cornerRadius = 10;
    verifyCodeView.layer.borderColor = [[UIColor blackColor] CGColor];
    [self addSubview:verifyCodeView];
    [verifyCodeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(phoneView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(phoneView.mas_left).mas_offset(0);
        make.height.mas_equalTo(36);
        make.width.mas_equalTo(phoneView.mas_width).multipliedBy(0.6);
    }];
    
    _verify_code_field = [UITextField new];
    _verify_code_field.placeholder = @"验证码";
    _verify_code_field.keyboardType = UIKeyboardTypeNumberPad;
    _verify_code_field.font = [UIFont systemFontOfSize:17.0f];
    [verifyCodeView addSubview:_verify_code_field];
    [_verify_code_field mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
    }];
    
    UILabel *step_title_label = [UILabel new];
    step_title_label.text = @"步骤: 1  /  3";
    step_title_label.textColor = [UIColor grayColor];
    step_title_label.textAlignment = NSTextAlignmentCenter;
    [self addSubview:step_title_label];
    [step_title_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(phoneView.mas_top).mas_offset(-20);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(20);
    }];
    
    _again_send_button = [UIButton new];
    _again_send_button.layer.masksToBounds = YES;
    _again_send_button.layer.borderWidth = 1;
    _again_send_button.layer.cornerRadius = 10;
    _again_send_button.layer.borderColor = [[UIColor blackColor] CGColor];
    [_again_send_button setTitle:@"发送" forState:UIControlStateNormal];
    [_again_send_button addTarget:self action:@selector(clickSendVerifyButton:) forControlEvents:UIControlEventTouchUpInside];
    [_again_send_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _again_send_button.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    [self addSubview:_again_send_button];
    [_again_send_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(phoneView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(verifyCodeView.mas_right).mas_offset(10);
        make.right.mas_equalTo(phoneView.mas_right).mas_offset(0);
        make.height.mas_equalTo(36);
    }];
    
    _next_step_button = [UIButton new];
    [_next_step_button setTitle:@"下一步" forState:UIControlStateNormal];
    [_next_step_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _next_step_button.titleLabel.font = [UIFont systemFontOfSize:24.0f];
    [_next_step_button addTarget:self action:@selector(clickNextStepButton:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_next_step_button];
    [_next_step_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(verifyCodeView.mas_bottom).mas_offset(20);
        make.centerX.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(100, 40));
    }];
}

- (void)recoverTime
{
    [_timer invalidate];
    _again_send_button.enabled = YES;
    [_again_send_button setTitle:@"发送" forState:UIControlStateNormal];
}

- (void)startTime
{
    _again_send_button.enabled = NO;
    _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownMethod) userInfo:nil repeats:YES];
}

- (void)countDownMethod{
    [_again_send_button setTitle:[NSString stringWithFormat:@"重新获取(%lu)",_second] forState:UIControlStateNormal];
    _second--;
    if(_second==0){
        _again_send_button.enabled = YES;
        [_again_send_button setTitle:@"重新获取" forState:UIControlStateNormal];
        [_timer invalidate];
    }
}

- (void)clickSendVerifyButton:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(clickSendVerifyWithPhone:)]) {
        [self.delegate clickSendVerifyWithPhone:_phone_field.text];
    }
}

- (void)clickNextStepButton:(UIButton *)button
{
    if ([self.delegate respondsToSelector:@selector(clickNextStepWithPhone:verifyCode:)]) {
        [self.delegate clickNextStepWithPhone:_phone_field.text verifyCode:_verify_code_field.text];
    }
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_phone_field resignFirstResponder];
    [_verify_code_field resignFirstResponder];
}

@end
